//
//  NiubiStyleCell_baoliao.m
//  danpin
//
//  Created by chuangjia on 16/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "NiubiStyleCell_baoliao.h"
#import "AppDelegate.h"

#import "zapi.h"

#import "viewconfig.h"


@interface NiubiStyleCell_baoliao()

@property(nonatomic,strong) UIView * backView;
@property(nonatomic,strong) TTTAttributedLabel *title;
@property(nonatomic,strong)UIImageView *titleIcon;
@property(nonatomic,strong)UILabel *specialComment;
@property(nonatomic,strong)UILabel *priceValue;
@property(nonatomic,strong)UILabel *zheValue;
@property(nonatomic,strong)UILabel *lbTopmost;
@property(nonatomic,strong)UIButton * btnComment;
@property(nonatomic,strong)UILabel * lbTime;

@end

@implementation NiubiStyleCell_baoliao

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithRed:0.967 green:0.967 blue:0.967 alpha:1];
        
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.cornerRadius = 3;//(值越大，角就越圆)
        _backView.layer.masksToBounds = YES;
        
        _backView.frame = CGRectMake(PHOTO_TIME_PADDING, 0, DEVICE_WIDTH - PHOTO_FRAME_WIDTH, BAOLIAO_CELL_HEIGHT);
        
        [self.contentView addSubview:_backView];
        
        
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkGrayColor];//UIColorFromRGB(0x808080);
        _title.textAlignment = NSTextAlignmentLeft;
        _title.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        
        _title.numberOfLines=0;
        _title.font = FONT(13);
        [self.contentView addSubview:_title];
        
        
        _titleIcon =[[UIImageView alloc] initWithFrame:CGRectZero];
        
        _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_TIME_PADDING, ICON_WIDTH, ICON_WIDTH);
        _titleIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_titleIcon];
        
        _lbTopmost = [[UILabel alloc]initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH,20, 16)];
        
        _lbTopmost.backgroundColor = [UIColor colorWithRed:0.937 green:0.937 blue:0.937 alpha:1];
        _lbTopmost.textColor = [UIColor darkGrayColor];//[UIColor whiteColor];
        _lbTopmost.font = FONT(10);
        _lbTopmost.textAlignment = NSTextAlignmentCenter;
        _lbTopmost.layer.cornerRadius = 3;//(值越大，角就越圆)
        _lbTopmost.layer.masksToBounds = YES;
        _lbTopmost.layer.borderColor = [UIColor grayColor].CGColor;
        _lbTopmost.layer.borderWidth = 0.5f;
        _lbTopmost.alpha = 0.7f;
        
        [self.contentView addSubview:_lbTopmost];
        
        _specialComment =[[UILabel alloc] initWithFrame:CGRectZero];
        _specialComment.backgroundColor = [UIColor clearColor];
        _specialComment.textColor = [UIColor grayColor];
        _specialComment.textAlignment = NSTextAlignmentLeft;
        _specialComment.font =  FONT(12);
        _specialComment.numberOfLines=0;
        [self.contentView addSubview:_specialComment];
        
        _zheValue =[[UILabel alloc] initWithFrame:CGRectZero];
        _zheValue.backgroundColor = [UIColor clearColor];
        _zheValue.textColor = RED_COLOR;
        _zheValue.textAlignment = NSTextAlignmentLeft;
        _zheValue.font = FONT_BOLD(14);
        _zheValue.numberOfLines=0;
//        _zheValue.layer.cornerRadius = PHOTO_FRAME_WIDTH;
//        _zheValue.layer.masksToBounds = YES;
        [self.contentView addSubview:_zheValue];
        
        _priceValue =[[UILabel alloc] initWithFrame:CGRectZero];
        _priceValue.backgroundColor = [UIColor clearColor];
        _priceValue.textColor = RED_COLOR;
        _priceValue.textAlignment = NSTextAlignmentLeft;
        _priceValue.font = FONT_BOLD(14);
        _priceValue.numberOfLines=0;
        [self.contentView addSubview:_priceValue];
//        self.priceValue.frame =CGRectMake(PHOTO_FRAME_WIDTH + ICON_WIDTH + PHOTO_FRAME_WIDTH,
//                                          PHOTO_FRAME_WIDTH + ICON_WIDTH + PHOTO_FRAME_WIDTH - PHOTO_FRAME_WIDTH*4,
//                                          DEVICE_WIDTH-ICON_WIDTH-PHOTO_FRAME_WIDTH*3,
//                                          PHOTO_FRAME_WIDTH*3);
        
        
        
        _lbTime =[[UILabel alloc] initWithFrame:CGRectZero];
        _lbTime.backgroundColor = [UIColor clearColor];
        _lbTime.textColor =  [UIColor grayColor];
        _lbTime.font =  FONT(11);
        _lbTime.numberOfLines=0;
        _lbTime.textAlignment = NSTextAlignmentRight;
        
        self.lbTime.frame =CGRectMake(DEVICE_WIDTH - 120,
                                      PHOTO_FRAME_WIDTH + ICON_WIDTH + PHOTO_FRAME_WIDTH - PHOTO_FRAME_WIDTH*3.5,
                                      105,
                                      PHOTO_FRAME_WIDTH*2);
        
        [self.contentView addSubview:_lbTime];
        
        
        
        
        _btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnComment.backgroundColor = [UIColor clearColor];
        [_btnComment setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _btnComment.titleLabel.font = FONT(11);
        
        [_btnComment setImage:[UIImage imageNamed:@"pl_icon.png"] forState:UIControlStateNormal];
        
        //设置按钮偏移
        _btnComment.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _btnComment.imageEdgeInsets = UIEdgeInsetsMake(5, 0, 3, 0);
        _btnComment.titleEdgeInsets = UIEdgeInsetsMake(6, -5, 5, 0);
        _btnComment.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_btnComment];
        
        
        
    }
    
    return self;
}

- (void)dealloc
{
    self.backView=nil;
    self.title=nil;
    self.titleIcon=nil;
    self.specialComment=nil;
    self.priceValue =nil;
    self.zheValue=nil;
    self.lbTopmost=nil;
    self.btnComment=nil;
    self.lbTime=nil;
    
}

- (NSMutableAttributedString*) createAttributedStr:(NSString*) strText fontSize:(int)nFontSize
{
    if (strText==nil) {
        strText =@"";
    }
    //创建AttributeString
    NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:strText];
    [attributedString addAttribute:NSFontAttributeName value:FONT(14)  range:NSMakeRange(0,[attributedString length])];
    //设置字体颜色
    [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
    CGFloat lineSpace = 2.0f;
    CTParagraphStyleSetting lineSpaceStyle;
    lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
    lineSpaceStyle.valueSize = sizeof(lineSpace);
    lineSpaceStyle.value =&lineSpace;
    
    //创建设置数组
    CTParagraphStyleSetting settings[ ] ={lineSpaceStyle};
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,1);
    
    //给文本添加设置
    [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
    return attributedString;
}


/// 根据数据模型来显示内容
- (void)showInfo:(id)model  indexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    if(dict == nil|| [dict isKindOfClass:[NSDictionary class]] == NO)
        return;
    self.data =model;
    
    NSString *tag_name =[dict stringForKey:@"tag_name" withDefaultValue:@""];//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"tag_name"]] replaceNullString];
//    NSLog(@"=====tag_name======>%@",tag_name);
    if(tag_name.length>0)
    {
        _lbTopmost.hidden = NO;
        CGSize sizeName  = [tag_name boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(10)}context:nil].size;
        _lbTopmost.frame =CGRectMake(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH,sizeName.width+10, 16);
        _lbTopmost.text = tag_name;
    }
    else
    {
        _lbTopmost.text =@"";
        _lbTopmost.hidden = YES;
    }
    

    
    //    __weak typeof (self) wSelf = self;
    NSString *title =[dict stringForKey:@"post_title" withDefaultValue:@""];//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"post_title"]] replaceNullString];
    if(title.length>0)
    {
        self.title.attributedText = [self createAttributedStr:title fontSize:14];
        float fheight =  [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH-ICON_WIDTH - PHOTO_FRAME_WIDTH*3) str:self.title.attributedText];
        if (fheight> 70)
        {
            fheight= 70;
        }
        self.title.frame =CGRectMake(PHOTO_FRAME_WIDTH*2 + ICON_WIDTH, PHOTO_TIME_PADDING, DEVICE_WIDTH-ICON_WIDTH-PHOTO_FRAME_WIDTH*3, fheight);
        
        NSMutableString *strComment = [[NSMutableString alloc]init];
        NSString *comment =[dict stringForKey:@"post_special" withDefaultValue:@""];//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"post_special"]] replaceNullString];
        
        if (comment.length == 0)
        {
            comment =[dict stringForKey:@"category" withDefaultValue:@""];//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"category"]] replaceNullString];
            _specialComment.textColor = [UIColor grayColor];
        }
        else
        {
            //有编辑的特别备注，显示成浅红色（西瓜红）
            _specialComment.textColor = [UIColor brownColor];
        }
        
        if (comment.length>0)
        {
            
            [strComment appendString:comment];
          
            NSDictionary *shopDict = [dict dictionaryObjectForKey:@"shop"];
            if (shopDict && [shopDict isKindOfClass:[NSDictionary class]]) {
                NSString *shopName =[shopDict stringForKey:@"name" withDefaultValue:@""];//;[[NSString stringWithFormat:@"%@", [shopDict objectForKey:@"name"]] replaceNullString];
                if (shopName.length>0) {
                    if(strComment.length > 0)
                    {
                        [strComment appendString:@", "];
                    }
                    //shopName = [NSString stringWithFormat:@", %@",shopName];
                    [strComment appendString:shopName];
                }
                
                //todo：亚马逊字样应该从服务器获取 strWebsite
                _specialComment.text = strComment;
                _specialComment.frame =CGRectMake(PHOTO_FRAME_WIDTH*2 + ICON_WIDTH, fheight + PHOTO_FRAME_WIDTH, DEVICE_WIDTH-ICON_WIDTH-PHOTO_FRAME_WIDTH*3, PHOTO_FRAME_WIDTH*2);
            }
            
  
        }
        else
        {
            _specialComment.text =@"";
        }
        strComment = nil;
    }
    CGFloat xpoint = PHOTO_FRAME_WIDTH + ICON_WIDTH + PHOTO_FRAME_WIDTH;
    NSString *zhestr =@"6.8折";//[dict stringForKey:@"discount" withDefaultValue:@""];
    if (zhestr.length>0) {
        self.zheValue.frame =CGRectMake(xpoint,
                                        PHOTO_FRAME_WIDTH + ICON_WIDTH + PHOTO_FRAME_WIDTH - PHOTO_FRAME_WIDTH*4,
                                        PHOTO_FRAME_WIDTH*3.5,
                                        PHOTO_FRAME_WIDTH*3);
        self.zheValue.text =zhestr;
        xpoint =self.zheValue.frame.size.width+self.zheValue.frame.origin.x+PHOTO_FRAME_WIDTH;
        
    }
   
    NSString *pricestr =[dict stringForKey:@"price" withDefaultValue:@""];//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"price"]] replaceNullString];
    if (pricestr.length>0)
    {
        NSString *prices = [NSString stringWithFormat:@"%@ (%@)",pricestr,@"6.8折"];
        self.priceValue.text =prices;
        self.priceValue.frame =CGRectMake(xpoint,
                                        PHOTO_FRAME_WIDTH + ICON_WIDTH + PHOTO_FRAME_WIDTH - PHOTO_FRAME_WIDTH*4,
                                        DEVICE_WIDTH-ICON_WIDTH-PHOTO_FRAME_WIDTH*3,
                                        PHOTO_FRAME_WIDTH*3);
        
    } else
    {
        self.priceValue.text =@"";
    }
    
    
    NSString *strTime =[dict stringForKey:@"time" withDefaultValue:@""];//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"time"]] replaceNullString];
    if (strTime.length>0)
    {
        self.lbTime.text =strTime;
    } else
    {
        self.lbTime.text =@"";
    }
    
    
    
    NSString * strCommentText  = @"";
    
    int nCount = [[dict numberStringForKey:@"comment_count"] intValue];
    if(nCount > 0)
        strCommentText = [NSString stringWithFormat:@"%d",  nCount];
   
    
    if([strCommentText length] > 0)
    {
        
        CGSize sizeTime =[strTime sizeWithAttributes:@{NSFontAttributeName:self.lbTime.font}];
        
        CGSize sizeComment =[strCommentText sizeWithAttributes:@{NSFontAttributeName:self.btnComment.titleLabel.font}];
        
        
        float commentWidth =   sizeComment.width + self.lbTime.frame.size.height + PHOTO_FRAME_WIDTH;
        
        _btnComment.frame = CGRectMake( DEVICE_WIDTH -  sizeTime.width - commentWidth - PHOTO_FRAME_WIDTH*1.5 , self.lbTime.frame.origin.y, commentWidth, self.lbTime.frame.size.height);
        
        [_btnComment setTitle:strCommentText forState:UIControlStateNormal];
        
        _btnComment.hidden = NO;
        
    }
    else
    {
        _btnComment.hidden = YES;
    }
    
//    [self layoutSubviews];
    [self setIcon:dict];
    

}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return BAOLIAO_CELL_HEIGHT;
}


- (void) setIcon:(id)model
{
    
    NSDictionary *dict =(NSDictionary*)model;
    if(dict == nil)
        return;
    
    self.titleIcon.image = nil;
    
    NSString *strImageUrl =[dict stringForKey:@"thumbnail" withDefaultValue:@""];;//[[NSString stringWithFormat:@"%@",[dict objectForKey:@"thumbnail"]] replaceNullString];
    if (strImageUrl.length>0 )
    {
        
        __weak typeof (self) wSelf = self;

        [self.titleIcon sd_setImageWithURL:[NSURL URLWithString:strImageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
         {
             if(image)
                 [wSelf.titleIcon.layer addAnimation:[CATransition animation] forKey:kCATransition];
             else
                 wSelf.titleIcon.image=[UIImage imageNamed:@"image.png"];
         }];

    }
    else
    {
        self.titleIcon.image=[UIImage imageNamed:@"image.png"];
    }
}


@end
